package kr.mir.frame;

import java.util.Iterator;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import kr.mir.util.config.Config;
import kr.mir.util.config.ConfigNode;
import kr.mir.util.parser.XmlParser;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6213172311472571881L;
	
	private BasicView view;
	private BasicEvent event;
	
	private Config config;
	
	public MainFrame(String title) {
		super(title);
		init();
	}
	
	private void init() {
		// TODO Auto-generated method stub
		XmlParser parser = new XmlParser("bin/kr/mir/resource/JavaTest.xml");
		config = parser.getConfig();
		
		String[] size = config.getValue("size").split(":");
		String[] start = config.getValue("start").split(":");
		
		int startX = Integer.parseInt(start[0]);
		int startY = Integer.parseInt(start[1]);
		
		int width = Integer.parseInt(size[0]);
		int height = Integer.parseInt(size[1]);
		
		setBounds(startX, startY, width, height);
		
		if(config.isChildren()) {
			Iterator<ConfigNode> iter = config.getChildren().iterator();
			while(iter.hasNext()) {
				Config child = (Config)iter.next();
				switch(child.getType()) {
				case "MenuBar" :
					JMenuBar menuBar = new JMenuBar();
					insertMenu(menuBar, child);
					setJMenuBar(menuBar);
					break;
				case "View" :
					try {
						this.view = (BasicView)Class.forName(child.getValue("class")).newInstance();
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						this.view = new BasicView();
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						this.view = new BasicView();
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						this.view = new BasicView();
						e.printStackTrace();
					}
					add(this.view);
					break;
				case "Event" :
					try {
						this.event = (BasicEvent)Class.forName(child.getValue("class")).newInstance();
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						this.event = new BasicEvent();
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						this.event = new BasicEvent();
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						this.event = new BasicEvent();
						e.printStackTrace();
					}
					break;
				default :
					break;
				}
			}
		}
	}

	private void insertMenu(JComponent parent, Config config) {
		// TODO Auto-generated method stub
		if(config.isChildren()) {
			Iterator<ConfigNode> iter = config.getChildren().iterator();
			while(iter.hasNext()) {
				Config child = (Config) iter.next();
				switch(child.getType()) {
				case "Menu" :
					JMenu menu = new JMenu(child.getValue("name"));
					insertMenu(menu, child);
					parent.add(menu);
					break;
				case "MenuItem" :
					JMenuItem menuItem = new JMenuItem(child.getValue("name"));
					menuItem.addActionListener(event);
					parent.add(menuItem);
					break;
				default : 
					break;
				}
			}
		}
	}
	

	/**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new MainFrame("FrameTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Display the window.
        //frame.setSize(450, 260);
        frame.setVisible(true);
    }
    
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}
}
